﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ZLDC.Common.Estate;

namespace ZLDC.Estate.TM
{
    public partial class SM_Accredit : _PageBase
    {
        protected string strRoleList = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int recount = 0;
                DataTable dt = ZLDC.Topo.BLL.Estate.T_s_pdmBLL.GetPagerT_s_RoleList(" where 1=1 and id=" + Request.QueryString["RoleID"].Trim(), 1, 10, out recount);
                strRoleList = dt.Rows[0]["pdm"].ToString();


                DataBindPdm();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        protected void DataBindPdm()
        {
            dlistData.DataSource = ZLDC.Topo.BLL.Estate.T_s_pdmBLL.GetT_s_pdmAllParentMenu("");
            dlistData.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dlistData_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemIndex >= 0)
            {
                CheckBoxList chkList = e.Item.FindControl("chkList") as CheckBoxList;
                HiddenField txtMdl = e.Item.FindControl("txtMdl") as HiddenField;
                DataTable dt = ZLDC.Topo.BLL.Estate.T_s_pdmBLL.GetT_s_pdmList(" 1=1 and mdl='" + txtMdl.Value.Trim() + "' order by isMenu desc");
                string strHTML = string.Empty;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strHTML += GetChkList(dt.Rows[i]["pdmPos"].ToString(), dt.Rows[i]["pdm"].ToString(), dt.Rows[i]["isMenu"].ToString());
                }
                Literal ltChkList = e.Item.FindControl("ltChkList") as Literal;
                Literal ltMdl = e.Item.FindControl("ltMdl") as Literal;
                ltMdl.Text = "<span onclick=\"hidDiv('" + e.Item.ItemIndex.ToString() + "')\" style=\"color:blue;cursor:hand;\"><img src='../../images/WebResourceJIAN.jpg' id='img" + e.Item.ItemIndex.ToString() + "' /> " + txtMdl.Value.Trim() + "</span>";
                ltChkList.Text = "<br/><div id='div" + e.Item.ItemIndex.ToString() + "' style='display:block'>" + strHTML + "</div>";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected string GetChkList(string pdmPos, string strText, string FontColor)
        {
            string strHTML = string.Empty;
            string strflg = "";
            try
            {
                strflg = strRoleList.Substring(Convert.ToInt32(pdmPos) - 1, 1);
            }
            catch { }

            string strChecked = strflg == "1" ? "checked" : "";
            if (FontColor == "True")
                strHTML = "<span style=\"width:180px;color:blue;background-color:Silver;\" nowrap><input type='checkbox' value='" + pdmPos + "' " + strChecked + " />" + strText + "</span>";
            else
                strHTML = "<span style='width:180px;' nowrap><input type='checkbox' value='" + pdmPos + "' " + strChecked + "  />" + strText + "</span>";

            return strHTML;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveData_Click(object sender, EventArgs e)
        {
            string strValue = txtValue.Value.Trim();
            string strGetValue = string.Empty;
            for (int i = 1; i <= strValue.Split('|').Length + 1; i++)
            {
                strGetValue += GetValue(i);
            }
            ZLDC.Topo.BLL.Estate.T_s_pdmBLL.UpdateRolePdm(Request.QueryString["RoleID"].Trim(), strGetValue);

            
            // 记录操作日志
            int RoleID = _Convert.ToInt(Request.QueryString["RoleID"].Trim(), 0);
            ZLDC.Topo.BLL.Estate.OperationlogBLL.p_operationlog_insert("[192.168.168.80].zlhq.kfb.zldc_t_s_Role", RoleID, "权限设置", "修改角色权限,RoleID:" + RoleID);

            //清空缓存
            _Cache.Remove("menu_" + RoleID);
            _Cache.Remove("rolelist_" + RoleID);

            Response.Redirect("SM_RoleList.aspx");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        protected string GetValue(int position)
        {
            string strValue = "|" + txtValue.Value.Trim() + "|";
            string strGetValue = string.Empty;
            if (strValue.IndexOf("|" + position + "," + "0" + "|") > -1)
                strGetValue = "0";
            else if (strValue.IndexOf("|" + position + "," + "1" + "|") > -1)
                strGetValue = "1";
            else
                strGetValue = "0";

            return strGetValue;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("SM_RoleList.aspx");
        }
    }
}
